package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.PushDataV4;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.event.model.SysSettingChangeEvent;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class SysSettingChangeEventListener implements ApplicationListener<SysSettingChangeEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(SysSettingChangeEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;


    @Override
    @Async
    public void onApplicationEvent(SysSettingChangeEvent event) {

        logger.debug("driver SysSettingChangeEvent event:{}", "");

        Setting setting = (Setting) event.getSource();
        Long companyId = event.getCompanyId();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("allowRushOrder", setting.isAllowRushOrder());
        map.put("allowWorkCar", setting.isAllowWorkCar());
        map.put("allowWorkCarZhuandan", setting.isAllowWorkCarZhuandan());
        map.put("allowDriverZhuandan", setting.isAllowDriverZhuandan());
        map.put("allowDriverCancel", setting.isAllowDriverCancel());
        map.put("mustToPlace", setting.isMustToPlace());
        map.put("mustMemberInfo", setting.isMustMemberInfo());
        map.put("allowModifyCash", setting.isAllowModifyCash());
        map.put("allowBaoxiao", setting.isAllowBaoxiao());
        map.put("insurancesShow", setting.isInsurancesShow());
        map.put("openHelpPay", setting.isOpenHelpPay());
        map.put("justPrice", setting.isJustPrice());

        List<Driver> drivers = driverService.listValid(companyId);

        if (!CollectionUtils.isEmpty(drivers)) {
            Integer time = 0;
            for (Driver driver : drivers) {

                try {
                    ++time;
                    PushDataV4 pushDataV4 = new PushDataV4();
                    pushDataV4.setCode("0X01");
                    pushDataV4.setTime(time / 10);
                    Map<String, String> extra = new HashMap<String, String>();
                    extra.put("content", "系统设置已变更");
                    pushDataV4.setExtra(extra);

                    boolean getuiOnline = false;
                    if (StringUtils.isNotBlank(driver.getUserID())) {
                        getuiOnline = GeTuiEmployV4
                            .getClientOnline(driver.getUserID(), settingUtils.get());
                    }

                    if (getuiOnline) {
                        GeTuiEmployV4.push(driver, pushDataV4, settingUtils.get());
                    } else {
                        if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                            JiGuangEmployV4.push(driver, pushDataV4, settingUtils.get());
                        }
                    }

                } catch (IOException e) {
                    logger.debug("push error : " + e.getMessage());
                }

            }
        }


    }

}
